package com.acwing.partition11;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 * @author `RKC`
 * @date 2021/12/10 20:07
 */
public class AC1012友好城市 {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] s = reader.readLine().split("\\s+");
        int n = Integer.parseInt(s[0]);
        int[][] edges = new int[n][2];
        for (int i = 0; i < n; i++) {
            s = reader.readLine().split("\\s+");
            edges[i][0] = Integer.parseInt(s[0]);
            edges[i][1] = Integer.parseInt(s[1]);
        }
        writer.write(String.valueOf(dynamicProgrammingLIS(edges)));
        writer.flush();
    }

    private static int dynamicProgrammingLIS(int[][] edges) {
        int n = edges.length, answer = 0;
        //排序预处理，以北方城市为标准进行排序
        Arrays.sort(edges, Comparator.comparingInt(o -> o[0]));
        int[] dp = new int[n];
        //最长上升子序列，已经固定了北方，所以我们找南方的上升序列
        for (int i = 0; i < n; i++) {
            dp[i] = 1;
            for (int j = 0; j < i; j++) {
                if (edges[i][1] > edges[j][1]) dp[i] = Math.max(dp[i], dp[j] + 1);
                answer = Math.max(answer, dp[i]);
            }
        }
        return answer;
    }

    private static int dynamicProgrammingLCS(int[][] edges) {
        int n = edges.length, northIndex = 0, southIndex = 0;
        int[] northCities = new int[n], southCities = new int[n];
        //映射北方城市对应的一个南方友好城市
        Map<Integer, Integer> citiesMap = new HashMap<>(n);
        for (int[] edge : edges) {
            northCities[northIndex++] = edge[0];
            southCities[southIndex++] = edge[1];
            citiesMap.put(edge[0], edge[1]);
        }
        Arrays.sort(northCities);
        Arrays.sort(southCities);
        int[][] dp = new int[n + 1][n + 1];
        //最长公共子序列，这里的判断是根据是否成对应的映射关系，而不是相等
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                int north = northCities[i - 1], south = southCities[j - 1];
                if (citiesMap.get(north) == south) dp[i][j] = dp[i - 1][j - 1] + 1;
                else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
        return dp[n][n];
    }
}
